import UIKit

class TF_LivingTableViewCell: UITableViewCell {

    @objc public var viewWidth:CGFloat = 0
    @objc public var viewHeight:CGFloat = 0
    @objc public let avatar = UIImageView()
    @objc public let msgView = UIImageView()
    @objc public let msg = UILabel()
    @objc public let name = UILabel()
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
      
        self.contentView.addSubview(self.avatar)
        self.contentView.addSubview(self.msgView)

        self.msg.numberOfLines = 0;
        self.msgView.addSubview(self.msg)
        
        self.name.numberOfLines = 0;
        self.contentView.addSubview(self.name)
    }
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override func layoutSubviews() {
        super.layoutSubviews()
    }
    
    @objc public var model:TFChatData?{
        didSet {
            
            if ((model) != nil)
            {
                var strinColor = UIColor(red: 255/255.0, green: 255/255.0, blue: 255/255.0, alpha: 1.0)
                
                //是助教客服 左
                if (model!.role == .roleAdmin) {
                    strinColor = UIColor(red: 27/255.0, green: 57/255.0, blue: 71/255.0, alpha: 1.0)
                
                }
                
                let font = UIFont.PingFang(size: 15)
           
                let string = TalkfunSharedTools.getAttributedText(model!.msg, fontSize: font, foregroundColor: strinColor)
                
                
                let testString = TalkfunSharedTools.getAttributedText("计算专用", fontSize: font, foregroundColor: strinColor)
                
                let testSize = TalkfunSharedTools.getSizeLabel(labelString: testString, labelWidth:self.viewWidth - 62 - 28, labelfont: font)
                  
                
                
                let size = TalkfunSharedTools.getSizeLabel(labelString: string, labelWidth:self.viewWidth - 62 - 28, labelfont: font)
                
                
                //左右间距
                let msgViewSpacing:CGFloat = 16
                //是助教客服 左
                if (model!.role == .roleAdmin) {
                    
                    self.avatar.frame = CGRect(x: 15, y: 10, width: 22, height: 22)
                    
                    let nameX:CGFloat = 45
                    self.name.frame = CGRect(x: nameX, y: self.avatar.frame.origin.y, width: self.viewWidth - nameX - 12, height: self.avatar.frame.size.height)
                    self.name.textAlignment = .left;
                    
                    
                    self.msgView.backgroundColor = UIColor(red: 244/255.0, green: 244/255.0, blue: 248/255.0, alpha: 1.0)
                  
                  
                    //字符宽度 + 两个 16
                let msgViewW:CGFloat = size.width + msgViewSpacing + msgViewSpacing;
                let msgViewH:CGFloat = size.height + 10 + 7
                    
                    self.msgView.frame = CGRect(x: nameX, y: self.name.frame.maxY + 4, width: msgViewW, height: msgViewH)
                   
                    
                    self.msg.frame = CGRect(x: msgViewSpacing, y: 10, width: size.width, height: size.height)
                 
                    self.msg.textAlignment = .left;
                    
               
                }else{
                    
                    let avatarW:CGFloat = 22;
                    self.avatar.frame = CGRect(x: self.viewWidth - avatarW - 12, y: 10, width: avatarW, height: avatarW)
                  
                    
                    //学员
                    self.name.frame = CGRect(x: 12, y: self.avatar.frame.origin.y, width: self.viewWidth - 45 - 12, height: self.avatar.frame.size.height)
                    
                    self.name.textAlignment = .right;
                
                    self.msgView.backgroundColor = UIColor(red: 65/255.0, green: 150/255.0, blue: 235/255.0, alpha: 1.0)
                

                    self.msg.textColor = UIColor(red: 255/255.0, green: 255/255.0, blue: 255/255.0, alpha: 1.0)
              
                  let msgViewX:CGFloat = self.viewWidth - 45 ;
                  
                    //字符宽度 + 两个 12
              let msgViewW:CGFloat = size.width + msgViewSpacing + msgViewSpacing;
                    
              let msgViewH:CGFloat = size.height + 10 + 7
                    
                    self.msgView.frame = CGRect(x: msgViewX - msgViewW, y: self.name.frame.maxY + 4, width: msgViewW, height: msgViewH)
                   
                    
                    self.msg.frame = CGRect(x: self.msgView.frame.size.width - size.width - msgViewSpacing, y: 10, width: size.width, height: size.height)
                    
                  
                    
                    //没有换行
                    if (self.msg.frame.size.height <= testSize.height ) {
                        self.msg.textAlignment = .right;
                    }else{
                        self.msg.textAlignment = .left;
                    }
                   
                    
                }
                
                self.msg.attributedText = string;
                
                let url:URL? = URL.init(string: model!.avatar as String)
                self.avatar.sd_setImage(with: url, completed: nil)

                self.msgView.layer.cornerRadius = 19;
                
                let time:NSString = model!.time as NSString
                
                let timeDou    = time.doubleValue
                
          
                let detaildate = NSDate.init(timeIntervalSince1970: timeDou)
                
                
                let  dformatter = DateFormatter ()

                if (TalkfunSharedTools.is24HourFormat() == true) {
                    dformatter.dateFormat =  "HH:mm"
                }else
                {
                    dformatter.dateFormat =  "hh:mm"
                }
                
                let timeText:NSString = dformatter.string(from: detaildate as Date) as NSString
         
                var nickname:NSString = ""
                let font13 = UIFont.PingFang(size: 13)
                //空格
                let space:NSString = " "
                if (model!.role == .roleAdmin) {
                    nickname = model!.nickname + (space as String) as NSString
                    
                    nickname = (nickname as String) + (timeText as String) as NSString
                 
                    let nameString = NSMutableAttributedString.init(string: nickname as String)
        
                    let nickname:NSString = model!.nickname as NSString
                    
                    let attr15: [NSAttributedString.Key : Any] = [.font: font as Any,]
                    
                    let NSRange15 = NSRange(location: 0, length: nickname.length )
                    nameString.addAttributes(attr15, range:NSRange15)
                    nameString.addAttribute(.foregroundColor, value: UIColor(red: 105/255.0, green: 113/255.0, blue: 120/255.0, alpha: 1.0), range:NSRange15)

                    //--------------------------------------
                    
                   
                    let newLength = nickname.length + space.length
                    
                    let attr13: [NSAttributedString.Key : Any] = [.font: font13 as Any,]
                    
                    let NSRange13 = NSRange(location:newLength, length: timeText.length )
                    nameString.addAttributes(attr13, range: NSRange13)
                    nameString.addAttribute(.foregroundColor, value: UIColor(red: 178/255.0, green: 189/255.0, blue: 196/255.0, alpha: 1.0), range: NSRange13)
             

                    self.name.attributedText = nameString;
                }else{
                    nickname = (timeText as String) + (space as String) as NSString
                    nickname = (nickname as String) + (model!.nickname as String) as NSString

                    let nameString = NSMutableAttributedString.init(string: nickname as String)
                    
                    //身份是学员
                    let attr13: [NSAttributedString.Key : Any] = [.font: font13 as Any,]
                    nameString.addAttributes(attr13, range: NSRange(location:0, length: timeText.length ))
                    nameString.addAttribute(.foregroundColor, value: UIColor(red: 178/255.0, green: 189/255.0, blue: 196/255.0, alpha: 1.0), range: NSRange(location: 0, length:  timeText.length))
                    
        
                    //--------------------------------------
                    let newLength = timeText.length + space.length
                    let nicknameLength = (model!.nickname as NSString).length
                    
                    let attr15: [NSAttributedString.Key : Any] = [.font: font as Any,]
                    
                    let NSRange15 = NSRange(location: newLength, length:  nicknameLength)
                    nameString.addAttributes(attr15, range: NSRange15)
                    nameString.addAttribute(.foregroundColor, value: UIColor(red: 105/255.0, green: 113/255.0, blue: 120/255.0, alpha: 1.0), range: NSRange15)
                    
                    self.name.attributedText = nameString;
                }
                
            }
            
        }
    }

}
